package com.example.lin.qq_home_project.activity;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.lin.qq_home_project.R;
import com.example.lin.qq_home_project.fragment.ContactFragment;
import com.example.lin.qq_home_project.fragment.ConversationFragment;
import com.example.lin.qq_home_project.fragment.NowFragment;
import com.example.lin.qq_home_project.fragment.PluginFragment;

public class QQHomeActivity extends AppCompatActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        initView();
        showFragmet(0);
    }

    private void initView() {
        initTabs();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_conversation:
                showFragmet(0);
                break;
            case R.id.ll_contact:
                showFragmet(1);
                break;
            case R.id.ll_now:
                showFragmet(2);
                break;
            case R.id.ll_plugin:
                showFragmet(3);
                break;
        }
    }


    private class TabItemView {
        LinearLayout ll;
        FrameLayout fl;
        ImageView iv;

        public void selected(boolean selected) {
            ll.setSelected(selected);//让真个布局(包括所有子View 都进入 selected的状态)
        }

    }

    private TabItemView[] tabItemViews = {new TabItemView(), new TabItemView(), new TabItemView(), new TabItemView()};

    private void initTabs() {
        tabItemViews[0].iv = findViewById(R.id.icon_conversation_face);
        tabItemViews[0].ll = findViewById(R.id.ll_conversation);
        tabItemViews[0].fl = findViewById(R.id.ll_conversation_fl);

        tabItemViews[1].iv = findViewById(R.id.icon_contact_eyes);
        tabItemViews[1].ll = findViewById(R.id.ll_contact);
        tabItemViews[1].fl = findViewById(R.id.ll_contact_fl);

        tabItemViews[2].iv = findViewById(R.id.icon_now);
        tabItemViews[2].ll = findViewById(R.id.ll_now);
        tabItemViews[2].fl = findViewById(R.id.ll_now_fl);

        tabItemViews[3].iv = findViewById(R.id.icon_plugin_start);
        tabItemViews[3].ll = findViewById(R.id.ll_plugin);
        tabItemViews[3].fl = findViewById(R.id.ll_plugin_fl);

        tabItemViews[0].ll.setOnClickListener(this);
        tabItemViews[1].ll.setOnClickListener(this);
        tabItemViews[2].ll.setOnClickListener(this);
        tabItemViews[3].ll.setOnClickListener(this);
    }

    private void showTab(int nextIndex) {
        tabItemViews[nextIndex].iv.clearAnimation();
        tabItemViews[nextIndex].selected(true);
//        ObjectAnimator scaleX = ObjectAnimator.ofFloat(tabItemViews[nextIndex].fl, "scaleX", 1, 0.5f, 1);
//        ObjectAnimator scaleY = ObjectAnimator.ofFloat(tabItemViews[nextIndex].fl, "scaleY", 1, 0.5f, 1);
//        AnimatorSet animatorSet = new AnimatorSet();
//        animatorSet.playTogether(scaleX,scaleY);
//        animatorSet.start();
        tabItemViews[nextIndex].fl.startAnimation(AnimationUtils.loadAnimation(this,R.anim.icon_scale));


        if (currentIndex != -1) {
            tabItemViews[currentIndex].selected(false);
            if (currentIndex == 0 || currentIndex == 1) {
                Animation animation ;
                if (nextIndex == 0) {
                    animation = AnimationUtils.loadAnimation(this, R.anim.icon_move_left);
                } else {
                    //打开动画
                      animation = AnimationUtils.loadAnimation(this, R.anim.icon_move_right);
                }
                tabItemViews[currentIndex].iv.startAnimation(animation);
            }
        }
    }


    public static final int TAB_NUM = 4;
    private Fragment[] fragments = new Fragment[TAB_NUM];

    private int currentIndex = -1;

    private void showFragmet(int nextIndex) {
        if (nextIndex == currentIndex)
            return;
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        if (fragments[nextIndex] == null) {
            ft.add(R.id.fl_container, fragments[nextIndex] = createFragment(nextIndex));
        } else {
            ft.show(fragments[nextIndex]);
        }
        if (currentIndex != -1) {
            ft.hide(fragments[currentIndex]);
        }
        ft.commit();
        showTab(nextIndex);
        currentIndex = nextIndex;
    }

    private Fragment createFragment(int nextIndex) {
        if (nextIndex < 0 || nextIndex > 3)
            throw new IndexOutOfBoundsException("你是不是傻,nextIndex只能是0-3");
        switch (nextIndex) {
            case 0:
                return new ConversationFragment();
            case 1:
                return new ContactFragment();
            case 2:
                return new NowFragment();
            case 3:
                return new PluginFragment();
        }
        return null;
    }


}
